package domain

import (
	"github.com/lab-online/internal/lab/constant"
	"github.com/lab-online/internal/lab/entity"
	"github.com/lab-online/internal/shared/code"
)

// checkEligibility 检查用户是否有权限进行实验, 管理员和教师无需加入班级即可进行实验
func (d *Domain) checkEligibility(lab entity.LabEntity) error {
	if exist, err := d.class.CheckClassIncludeStudent(
		lab.GetClassID(), lab.GetStudent(),
	); err != nil {
		return err
	} else if !exist {
		return constant.RESTErrNotInClass
	}

	class, err := d.class.GetClassByID(lab.GetClassID())
	if err != nil {
		return code.ErrorPicker(
			err, code.DatabaseClassNotFound, constant.RESTErrClassNotFound,
		)
	}

	exists, err := d.course.CheckCourseExperimentExists(class.GetCourseID(), lab.GetExperimentID())
	if err != nil {
		return err
	} else if !exists {
		return constant.RESTErrCourseLabNotFound
	}

	return nil
}
